package com.atguigu.daijia.order.client.fallback;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.model.entity.order.OrderMonitor;
import com.atguigu.daijia.model.entity.order.OrderMonitorRecord;
import com.atguigu.daijia.order.client.OrderMonitorFeignClient;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @create: 2024/9/28
 * @author: gyhe10
 * @description: OrderMonitorFeignClient 接口的降级实现工厂类
 **/
@Component
public class OrderMonitorFeignClientFallbackFactory implements FallbackFactory<OrderMonitorFeignClient> {
    @Override
    public OrderMonitorFeignClient create(Throwable cause) {
        return new OrderMonitorFeignClient() {
            @Override
            public Result<Boolean> saveOrderMonitorRecord(OrderMonitorRecord orderMonitorRecord) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<Long> saveOrderMonitor(OrderMonitor orderMonitor) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<OrderMonitor> getOrderMonitorByOrderId(Long orderId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<Boolean> updateOrderMonitor(OrderMonitor orderMonitor) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }
        };
    }
}
